package com.richard.base.net.interceptors

import com.richard.base.BaseApplication
import com.richard.base.net.utils.NetworkUtils
import okhttp3.CacheControl
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import java.util.concurrent.TimeUnit


/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/11/4     5:23 PM
 * 用途:
 ***************************************
 */
object CacheInterceptor {


    /**
     * 有网时候的缓存
     */
    val NetCacheInterceptor: Interceptor = object : Interceptor {
        @Throws(IOException::class)
        override fun intercept(chain: Interceptor.Chain): Response{

            val request: Request = chain.request()
            val response: Response = chain.proceed(request)
            val onlineCacheTime = 24 * 60 * 60 //在线的时候的缓存过期时间，如果想要不缓存，直接时间设置为0
            return response.newBuilder()
                .removeHeader("Pragma")
                .header("Cache-Control", "public, max-age=$onlineCacheTime")
                .build()
        }
    }

    /**
     * 没有网时候的缓存
     */
    val OfflineCacheInterceptor: Interceptor = object : Interceptor {
        @Throws(IOException::class)
        override fun intercept(chain: Interceptor.Chain): Response {
            var request: Request = chain.request()
            if (!NetworkUtils.isNetConnected(BaseApplication.instance)) {
                val offlineCacheTime = 2 * 24 * 60 * 60 //离线的时候的缓存的过期时间
                request =
                    request.newBuilder()
                        .removeHeader("Pragma")
                        .header(
                            "Cache-Control",
                            "public, only-if-cached, max-stale=$offlineCacheTime"
                        )
                        .build()
            }
            return chain.proceed(request)
        }
    }

    //cache
    var REWRITE_CACHE_CONTROL_INTERCEPTOR: Interceptor = object : Interceptor {
        @Throws(IOException::class)
        override fun intercept(chain: Interceptor.Chain): Response {
            val cacheBuilder = CacheControl.Builder()
            cacheBuilder.maxAge(1, TimeUnit.DAYS)
            cacheBuilder.maxStale(1, TimeUnit.DAYS)
            val cacheControl = cacheBuilder.build()
            var request: Request = chain.request()

            val maxAge = 60 * 60 // read from cache

            if (NetworkUtils.isNetConnected(BaseApplication.instance)) {
                request = request.newBuilder()
                    .header("Cache-Control", "public, max-age=$maxAge")
                    .build()
            } else {
                request = request.newBuilder().header("Cache-Control",
                    "public, only-if-cached, max-stale=$maxAge"
                ).build()
            }


            val originalResponse: Response = chain.proceed(request)
            return if (NetworkUtils.isNetConnected(BaseApplication.instance)) {

                originalResponse.newBuilder()
                    .header("Cache-Control", "public, max-age=$maxAge")
                    .build()
            } else {
                originalResponse.newBuilder()
                    .header("Cache-Control", "public, only-if-cached, max-stale=$maxAge")
                    .build()
            }
        }
    }

}